package com.sf.pathmemorymethod;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.BroadcastReceiver;
import android.content.ContentValues;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.sf.adapter.PathSettingAdapter;
import com.sf.util.Constant;
import com.sf.util.Mysqlitehelper;
import com.sf.util.Path;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

/**
 * Created by YSir on 2018/7/7.
 */
public class PathSettingActivity extends Activity {

    private Mysqlitehelper helper;
    private ListView lv;
    private PathSettingAdapter mAdapter;
    private List<Path> mDatas;

    private TextView tv_name;
    private Button btn_add_path;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_path_setting);

        initView();
        initData();

        // 更改后刷新页面
        IntentFilter filter = new IntentFilter("action.refreshActivity");
        registerReceiver(mRefreshBroadcastReceiver, filter);

    }

    // 更改后刷新页面
    BroadcastReceiver mRefreshBroadcastReceiver = new BroadcastReceiver() {

        @Override
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();
            if (action.equals("action.refreshActivity"))
            {
                refresh();
            }
        }
    };

    public void refresh() {

        onCreate(null);

    }


    private void initView(){
        lv = (ListView) findViewById(R.id.ps_list);
        tv_name = (TextView) findViewById(R.id.tv_name);

        btn_add_path = (Button) findViewById(R.id.btn_add_path);
        btn_add_path.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                showInfo();
            }
        });

    }

    private void initData()
    {
        mDatas = new ArrayList<Path>();
        // 查询数据
        helper = new Mysqlitehelper(PathSettingActivity.this);
        SQLiteDatabase db = helper.getWritableDatabase();
        Cursor cursor = db.query(Constant.TABLE_PATH,null,null,null,null,null,null);
        if (cursor == null){
            Log.i("tag","没有数据");
        }else {
            while (cursor.moveToNext()) {
                mDatas.add(new Path(cursor.getInt(cursor.getColumnIndex(Constant.ID)),cursor.getString(cursor.getColumnIndex(Constant.NAME))));
            }
        }
        db.close();

        mAdapter = new PathSettingAdapter(this,mDatas);
        lv.setAdapter(mAdapter);

    }

    public void showInfo(){

        // 创建对话框构建器
        AlertDialog.Builder builder = new AlertDialog.Builder(PathSettingActivity.this);
        // 获取布局
        View view2 = View.inflate(PathSettingActivity.this, R.layout.layout_add_pile, null);
        // 获取布局中的控件
        final EditText editname = (EditText) view2.findViewById(R.id.ed_name);
        final Button button = (Button) view2.findViewById(R.id.btn_edit);
        // 设置参数
        builder.setTitle("添加路线")
                .setView(view2);
        // 创建对话框
        final AlertDialog alertDialog = builder.create();
        button.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                // 插入数据
                helper = new Mysqlitehelper(PathSettingActivity.this);
                SQLiteDatabase db = helper.getWritableDatabase();
                ContentValues values = new ContentValues();
                values.put(Constant.NAME,editname.getText().toString()); //键值对 put(字段名称,值)
                long result = db.insert(Constant.TABLE_PATH,null,values);
                if (result>0){
                    Toast.makeText(PathSettingActivity.this,"插入数据成功",Toast.LENGTH_SHORT).show();
                    String name = editname.getText().toString();
                    Cursor cursor = db.query(Constant.TABLE_PATH,new String[]{"id","name"}, "name=?", new String[]{name},null,null,null);
                    if (cursor == null){
                        Log.i("tag","没有数据");
                    }else {
                        while (cursor.moveToNext()) {
                            mDatas.add(new Path(cursor.getInt(cursor.getColumnIndex(Constant.ID)),cursor.getString(cursor.getColumnIndex(Constant.NAME))));
                        }
                    }
                    mAdapter.notifyDataSetChanged();
                }else {
                    Toast.makeText(PathSettingActivity.this,"插入数据失败",Toast.LENGTH_SHORT).show();
                }
                db.close();

                alertDialog.dismiss();// 对话框消失
            }

        });
        alertDialog.show();
    }
}
